package com.mlming.springboot.service.impl;

import com.google.gson.Gson;
import com.mlming.springboot.consts.WallConst;
import com.mlming.springboot.dao.UserMapper;
import com.mlming.springboot.enums.ResponseEnum;
import com.mlming.springboot.form.UserForm;
import com.mlming.springboot.pojo.User;
import com.mlming.springboot.service.UserService;
import com.mlming.springboot.vo.ResponseVo;
import com.mlming.springboot.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseVo schoolNumCheck(String schoolNum) {
        String pattern = WallConst.SCHOOL_NUM_REGEX;
        if(schoolNum.length() != WallConst.SCHOOL_NUM_LENGTH || !Pattern.matches(pattern,schoolNum)) {
            return ResponseVo.error(ResponseEnum.SCHOOL_NUM_ERR);
        }
        return ResponseVo.success();
    }

    @Override
    public ResponseVo usernameCheck(String username) {
        if(username.length() > WallConst.USERNAME_MAX_LENGTH || username.length() < WallConst.USERNAME_MIN_LENGTH) {
            return ResponseVo.error(ResponseEnum.USERNAME_LENGTH_ERR);
        }
        return ResponseVo.success();
    }

    @Override
    public ResponseVo passwordCheck(String password) {
        String pattern = WallConst.PASSWORD_REGEX;
        if(password.length() > WallConst.PASSWORD_MAX_LENGTH || password.length() < WallConst.PASSWORD_MIN_LENGTH) {
            return ResponseVo.error(ResponseEnum.PASSWORD_LENGTH_ERR);
        }
        if(!Pattern.matches(pattern,password)) {
            return ResponseVo.error(ResponseEnum.PASSWORD__ERR);
        }
        return ResponseVo.success();
    }

    @Override
    public ResponseVo<UserVo> register(UserForm userForm) {
        // 首先验证用户名是否存在:
        int count = userMapper.countByUsername(userForm.getUsername());
        if(count > 0) {
            return ResponseVo.error(ResponseEnum.USERNAME_EXISTS);
        }
        // 再验证学号是否存在:
        count = userMapper.countBySchoolNum(userForm.getSchoolNum());
        if(count > 0) {
            return ResponseVo.error(ResponseEnum.SCHOOL_NUM_EXISTS);
        }
        // 对密码进行MD5加密
        userForm.setPassword(DigestUtils.md5DigestAsHex(userForm.getPassword().getBytes(StandardCharsets.UTF_8)));
        // 写入数据库
        User user = new User();
        BeanUtils.copyProperties(userForm,user);
        int res = userMapper.insertSelective(user);
        // 读出数据
        if(res > 0) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            return ResponseVo.success(userVo);
        } else {
            return ResponseVo.error(ResponseEnum.ERROR);
        }
    }

    @Override
    public ResponseVo<UserVo> login(UserForm userForm) {
        User user = userMapper.selectBySchoolNum(userForm.getSchoolNum());
        // 验证用户是否存在
        if(user == null) {
            return ResponseVo.error(ResponseEnum.USERNAME_NOT_EXISTS);
        }
        // 验证用户名:
        if(!(user.getUsername().equals(userForm.getUsername()))) {
            return ResponseVo.error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        // 验证密码
        if(!user.getPassword().equals(DigestUtils.md5DigestAsHex(userForm.getPassword().getBytes(StandardCharsets.UTF_8)))) {
            return ResponseVo.error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        // 如果都无误,根据接口文档,返回用户信息
        // 注意: password要屏蔽掉
        user.setPassword("");
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return ResponseVo.success(userVo);
    }
}
